package com.wan.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.wan.R
import com.wan.base.BaseBean
import com.wan.base.WanApp
import com.wan.entity.LoginEntity
import com.wan.uitls.APIServices
import com.wan.uitls.ApiManager
import com.wan.uitls.CommonUtils
import com.wan.uitls.RetrofitHelper
import kotlinx.android.synthetic.main.activity_resgiter.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.lang.StringBuilder

/**
 *注册
 */
class RegisterActivity : AppCompatActivity() {
    private val mThreadName = this.javaClass.name

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_resgiter)
        register_register_btn.setOnClickListener {
            registerMethod()
        }
    }

    private fun registerMethod() {
        val accountText = register_account_tit.text.toString()
        if (accountText.isBlank()) {
            CommonUtils.toastWord(WanApp.sContext, "账号为空")
            return
        }
        val passText = register_pass_tit.text.toString()
        if (passText.isBlank()) {
            CommonUtils.toastWord(WanApp.sContext, "密码为空")
            return
        }
        val repeatText = register_repeat_tit.text.toString()
        if (repeatText.isBlank()) {
            CommonUtils.toastWord(WanApp.sContext, "重复密码为空")
            return
        }
        if (repeatText != passText) {
            CommonUtils.toastWord(WanApp.sContext, "两次密码不一致")
            return
        }

        val manager = ApiManager(this)
        val call = manager.postRegisterData(accountText, passText, repeatText)
        call.enqueue(object : Callback<BaseBean<LoginEntity>> {
            override fun onFailure(call: Call<BaseBean<LoginEntity>>, t: Throwable) {
                Log.e(mThreadName, "onFailure : 失败==>${call.execute().body()}", t)
            }

            override fun onResponse(call: Call<BaseBean<LoginEntity>>, response: Response<BaseBean<LoginEntity>>) {
                Log.d(mThreadName, "onResponse : 成功==>${response.body()}")
                var entity = response.body()!!.data
                val cookie: StringBuilder = StringBuilder()
                response.headers().values("Set-Cookie").forEach {

                    Log.d(mThreadName, "onResponse : 单个cookie ==> $it")
                    cookie.append(it).append(";")
                }
                var realCookie = cookie.deleteCharAt(cookie.lastIndex).toString()
                Log.d(mThreadName, " onResponse : 存储的cookie ==> $realCookie")
                entity.token = realCookie
                entity.password = passText
                CommonUtils.saveLoginMessage(this@RegisterActivity,entity)
                CommonUtils.saveLoginState(this@RegisterActivity,true)
                finish()
            }
        })
    }
    override fun onDestroy() {
        super.onDestroy()
        RetrofitHelper.get().cancelCall(this)
    }

}
